const app = getApp();
const publicFun = require('/utils/public.js');
import { getJianSuGuanChaList, setJianSuDel } from '/utils/request'
Page({
  data: {
    hc: '',
    pageindex: 1,
    jsid: 0,
    jianSuList: [],
    passList: [
      '/images/my/icon_inPass.png', // 审核中
      '/images/my/icon_pass.png', // 通过
      '/images/my/icon_noPass.png' // 未通过
    ],
  },
  onLoad() {
    this.setData({
      jianSuList: [],
      systemInfo: app.globalData.systemInfo
    })
    my.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
    })
    // my.setNavigationBarTitle({
    //   title: '自带杯打卡记录'
    // });
    this.getJianSuList(this.data.pageindex);
  },
  detailClick(e) {
    let data = e.currentTarget.dataset.item;
    my.navigateTo({
      url: '/pages/byoDetail/byoDetail?id='+data.Id+'&jsid='+this.data.jsid
    });
  },
  async delBtn(e) {
    let data = e.currentTarget.dataset;
    try{
      my.showLoading();
      let params = {
        menuid: app.globalData.menuid,
        userid: app.globalData.userid,
        id: data.id,
        hc: this.data.hc,
      }
      const res = await setJianSuDel(params);
      my.hideLoading();
      if(res.S == 1){
        publicFun.showToast("删除成功");
        my.navigateBack();
      }
    }catch(e) {
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },

  async getJianSuList(pageindex) {
    try{
      my.showLoading();
      let params = {
        menuid: app.globalData.menuid,
        userid: app.globalData.userid,
        jiansuindustryid: this.data.jsid,
        pageindex: pageindex,
        pagesize: app.globalData.pagesize
      }
      const res = await getJianSuGuanChaList(params);
      my.hideLoading();
      if(res.S == 1){
        if(res.L.length > 0){
          res.L.forEach(item => {
            item.passImg = this.data.passList[Number(item.State)];
            this.data.jianSuList.push(item);
          })
          this.setData({
            jianSuList: this.data.jianSuList,
            hc: res.HC
          });
        }else{
          publicFun.showToast("已经加载到底啦")
        }
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败')
      }
    }catch(e) {
      my.hideLoading();
      publicFun.showToast('失败，请稍后再试')
    }
  },
  onReachBottom() {
    const newPage = this.data.pageindex + 1;
    this.setData({pageindex: newPage});
    this.getJianSuList(newPage)
  },
});
